import { _decorator, Button, Color, color, find, Label, Layout, ProgressBar, RichText, ScrollView, Sprite, Tween, tween, UIOpacity, UITransform, Widget } from 'cc';
import { $planet_BaseUI_poof$ } from '../../../frame/component/ui.base';
import $planet_DC_poof$ from '../data/data.center';
import $planet_Config_poof$ from '../manager/config.dynamic';
import $planet_GameAssets_poof$ from '../manager/game-assets';
import { $planet_Float_poof$ } from './float';
const { ccclass, property } = _decorator;

@ccclass('$planet_Free_poof$')
export class $planet_Free_poof$ extends $planet_BaseUI_poof$ {
	private $planet_ui_background_poof$: UITransform = null;
	private $planet_ui_view_poof$: UITransform = null;
	private $planet_ui_top_poof$: UITransform = null;
	private $planet_ui_bottom_poof$: UITransform = null;
	private $planet_view_poof$: ScrollView = null;

	private $planet_va_background_poof$: abd.$planet_cc_poof$.$planet_VecAssist_poof$;
	private $planet_va_view_poof$: abd.$planet_cc_poof$.$planet_VecAssist_poof$;
	private $planet_va_top_poof$: abd.$planet_cc_poof$.$planet_VecAssist_poof$;
	private $planet_va_bottom_poof$: abd.$planet_cc_poof$.$planet_VecAssist_poof$;

	protected lateUpdate(){
		if(this.select_record && this.select_record.update){
			if(this.select_record.count < 5) this.select_record.count++;
			else{
				this.$planet_showDetail_poof$(this.select_record.type);
				this.$planet_hideDetail_poof$(this.select_record.type=='symbol' ? 'elsb' : 'symbol');
			}
		}
	}

	protected $planet_initAfterOnLoad_poof$(): void {
		this.$planet_init_poof$();
		this.$planet_renderDeviceUI_poof$();
	}

	private $planet_init_poof$(){
		[this.$planet_ui_background_poof$, this.$planet_ui_view_poof$, this.$planet_ui_top_poof$, this.$planet_ui_bottom_poof$] = ['background', 'view', 'top', 'bottom'].map(name=>this.node.getChildByName(name).getComponent(UITransform));
		[this.$planet_va_background_poof$, this.$planet_va_view_poof$, this.$planet_va_top_poof$, this.$planet_va_bottom_poof$] = [this.$planet_ui_background_poof$, this.$planet_ui_view_poof$, this.$planet_ui_top_poof$, this.$planet_ui_bottom_poof$].map(ui=>new abd.$planet_cc_poof$.$planet_VecAssist_poof$(ui.node));
		this.$planet_view_poof$ = this.$planet_ui_view_poof$.getComponent(ScrollView);

		this.$planet_ui_background_poof$.node.setPosition(this.$planet_va_background_poof$.$planet_getPosition_poof$('$planet_over_right_poof$', '$planet_center_poof$'));
		this.$planet_ui_view_poof$.node.setPosition(this.$planet_va_view_poof$.$planet_getPosition_poof$('$planet_over_right_poof$', '$planet_center_poof$'));
		this.$planet_ui_top_poof$.node.setPosition(this.$planet_va_top_poof$.$planet_getPosition_poof$('$planet_center_poof$', '$planet_over_top_poof$'));
		this.$planet_ui_bottom_poof$.node.setPosition(this.$planet_va_bottom_poof$.$planet_getPosition_poof$('$planet_center_poof$', '$planet_over_bottom_poof$'));

		this.$planet_adaptUI_poof$();
		this.$planet_renderLocalUI_poof$();

		this.node.active = false;;
	}

	private $planet_adaptUI_poof$(){
		let wgt = this.node.getComponent(Widget);
		if(wgt) wgt.updateAlignment();
		let ui_background = this.node.getChildByName('background').getComponent(UITransform);
		let size = ui_background.getComponent(Sprite).spriteFrame.originalSize;
		let ratio = abd.$planet_size_poof$.$planet_exterRatio_poof$(size, abd.$planet_size_poof$.$planet_canvasSize_poof$);
		ui_background.setContentSize(size.width * ratio, size.height * ratio);

		let lay_content = this.$planet_view_poof$.content.getComponent(Layout);
		if(lay_content){
			lay_content.paddingTop = 143 + (abd.$planet_cc_poof$.$planet_is_long_screen_poof$ ? 100 : 0);
			lay_content.updateLayout();
		}
		//let wgt_view = this.$planet_ui_view_poof$.getComponent(Widget);
		//if(wgt_view){
		//	wgt_view.top = 143 + (abd.$planet_cc_poof$.$planet_is_long_screen_poof$ ? 100 : 0);
		//	wgt_view.updateAlignment();
		//}
	}

	protected $planet_listening_poof$(){

		let lbl_symbol = find('symbol/value/label', this.$planet_view_poof$.content).getComponent(Label);
		let lbl_elsb = find('elsb/value/label', this.$planet_view_poof$.content).getComponent(Label);

		let list = [];

		Promise.all([
			$planet_DC_poof$.$planet_follow_poof$('$planet_trigger_poof$', '$planet_UI_FREE_poof$', (o, type)=>{
				if(type===o) return void 0;
				if(type===undefined) this.$planet_leave_poof$();
				else this.$planet_enter_poof$(type);
			}, false),
			$planet_DC_poof$.$planet_follow_poof$('$planet_user_poof$', '$planet_symbol_poof$', (_, v)=>{
				lbl_symbol.string = $planet_DC_poof$.$planet_symbolString_poof$(v??$planet_DC_poof$.$planet_user_poof$.$planet_symbol_poof$);
			}),
			$planet_DC_poof$.$planet_follow_poof$('$planet_user_poof$', '$planet_elsb_poof$', (_, v)=>{
				lbl_elsb.string = String(v??$planet_DC_poof$.$planet_user_poof$.$planet_elsb_poof$);
			}),
		]).then(ls=>list.push(...ls));

		return list;
	}

	protected $planet__click_event__poof$: { [name: string]: (this: $planet_Free_poof$, button: Button) => void; } = {
		back(btn){
			btn.interactable = false;
			$planet_DC_poof$.$planet_trigger_poof$.$planet_UI_FREE_poof$ = undefined;
			$planet_DC_poof$.$planet_trigger_poof$.$planet_UI_HOME_poof$ = true;
		},
		free(btn){
			btn.interactable = false;
			if(this.hint_text) $planet_Float_poof$.$planet_Hint_poof$(this.hint_text).then(()=>btn.interactable = true);
			else btn.interactable = true;
		}
	}

	private $planet_enterBefore_poof$(){
		this.$planet___btn_map___poof$.get('back').interactable = true;
	}

	private $planet_enter_poof$(type:'symbol'|'elsb'){
		this.$planet_enterBefore_poof$();
		this.node.active = true;
		let duration = 0.5;
		let np = new abd.$planet_NudityPromise_poof$<0>();
		this.scheduleOnce(()=>np.$planet_resolve_poof$(0), duration+0.05);

		this.$planet_va_background_poof$.$planet_getPosition_poof$('$planet_right_poof$', '$planet_center_poof$');
		this.$planet_va_view_poof$.$planet_getPosition_poof$('$planet_center_poof$', '$planet_center_poof$');
		this.$planet_va_top_poof$.$planet_getPosition_poof$('$planet_center_poof$', '$planet_top_poof$');
		this.$planet_va_bottom_poof$.$planet_getPosition_poof$('$planet_center_poof$', '$planet_bottom_poof$');
		if(abd.$planet_cc_poof$.$planet_is_long_screen_poof$) this.$planet_va_top_poof$.$planet_vec_poof$.add3f(0, -100, 0);

		this.$planet_clearOption_poof$();
		this.selectOption(type, 1);

		Tween.stopAllByTarget(this.$planet_ui_background_poof$.node);

		tween(this.$planet_ui_background_poof$.node)
		.to(duration, {position: this.$planet_va_background_poof$.$planet_vec_poof$})
		.start();

		(<const>[
			[this.$planet_ui_view_poof$, this.$planet_va_view_poof$],
			[this.$planet_ui_top_poof$, this.$planet_va_top_poof$],
			[this.$planet_ui_bottom_poof$, this.$planet_va_bottom_poof$],
		]).forEach(([ui, va])=>{
			Tween.stopAllByTarget(ui.node);
			tween(ui.node).to(duration, {position: va.$planet_vec_poof$}, {easing: 'quartInOut'}).start();
		});

		return np.$planet_promise_poof$;
	}

	private $planet_leave_poof$(){
		let duration = 0.5;
		let np = new abd.$planet_NudityPromise_poof$<0>();
		this.scheduleOnce(()=>np.$planet_resolve_poof$(0), duration+0.05);

		this.$planet_va_background_poof$.$planet_getPosition_poof$('$planet_over_right_poof$', '$planet_center_poof$');
		this.$planet_va_view_poof$.$planet_getPosition_poof$('$planet_over_right_poof$', '$planet_center_poof$');
		this.$planet_va_top_poof$.$planet_getPosition_poof$('$planet_center_poof$', '$planet_over_top_poof$');
		this.$planet_va_bottom_poof$.$planet_getPosition_poof$('$planet_center_poof$', '$planet_over_bottom_poof$');

		Tween.stopAllByTarget(this.$planet_ui_background_poof$.node);
		tween(this.$planet_ui_background_poof$.node)
		.to(duration, {position: this.$planet_va_background_poof$.$planet_vec_poof$})
		.start();

		(<const>[
			[this.$planet_ui_view_poof$, this.$planet_va_view_poof$],
			[this.$planet_ui_top_poof$, this.$planet_va_top_poof$],
			[this.$planet_ui_bottom_poof$, this.$planet_va_bottom_poof$],
		]).forEach(([ui, va])=>{
			Tween.stopAllByTarget(ui.node);
			tween(ui.node).to(duration, {position: va.$planet_vec_poof$}, {easing: 'quartInOut'}).start();
		});

		np.$planet_promise_poof$.then(()=>this.node.active = false);

		return np.$planet_promise_poof$;
	}

	private $planet_renderLocalUI_poof$(){
		let ui_content = this.$planet_view_poof$.content.getComponent(UITransform);
		(<const>[
			['symbol', 10, $planet_DC_poof$.$planet_symbolString_poof$(), $planet_Config_poof$.$planet_otherConfig_poof$.$planet_freeSymbol_poof$],
			['elsb', 11, $planet_DC_poof$.$planet_user_poof$.$planet_elsb_poof$, $planet_Config_poof$.$planet_otherConfig_poof$.$planet_freeElsb_poof$]
		]).forEach(([name, index, value, list])=>{
			find(name+'/lbl-head', ui_content.node).getComponent(Label).string = $planet_GameAssets_poof$.$planet_getLangText_poof$(index);
			find(name+'/value/label', ui_content.node).getComponent(Label).string = String(value);
			let select = find(name+'/select', ui_content.node);
			list.forEach((el, i)=>select.getChildByName('lbl-option'+(i+1)).getComponent(Label).string = $planet_DC_poof$.$planet_symbolString_poof$(el.$planet_symbol_poof$));
		});

		let ui_btn_free = this.$planet___btn_map___poof$.get('free').getComponent(UITransform);
		let sframe_free_word = $planet_GameAssets_poof$.$planet_getLangTexture_poof$(2);
		let [R, r] = [ui_btn_free.contentSize, sframe_free_word.originalSize].map(s=>s.width / s.height);
		if(r > R){
			let ratio = abd.$planet_size_poof$.$planet_innerRatio_poof$(sframe_free_word.originalSize, ui_btn_free.contentSize) * 0.8;
			let sprite = ui_btn_free.node.getChildByName('word').getComponent(Sprite);
			sprite.spriteFrame = sframe_free_word;
			sprite.getComponent(UITransform).setContentSize(sframe_free_word.originalSize.width * ratio, sframe_free_word.originalSize.height * ratio);
		}else abd.$planet_cc_poof$.$planet_skinPeeler_poof$(ui_btn_free.node.getChildByName('word'), sframe_free_word, {height: true});

		this.$planet_ui_top_poof$.node.getChildByName('label').getComponent(Label).string = $planet_GameAssets_poof$.$planet_getLangText_poof$(9);

		let elsb_select = find('elsb/select', this.$planet_view_poof$.content);
		$planet_Config_poof$.$planet_otherConfig_poof$.$planet_freeElsb_poof$.forEach((el, i)=>{
			elsb_select.getChildByName('lbl-count'+(i+1)).getComponent(Label).string = el.$planet_countElsb_poof$.toString();
		});

		let icon_symbol = find('symbol/value/icon', this.$planet_view_poof$.content);
		abd.$planet_cc_poof$.$planet_skinPeeler_poof$(icon_symbol, $planet_GameAssets_poof$.$planet_getCounTexture_poof$(0), {height: true});
	}

	protected $planet_ClickBtn_poof$(btn: Button): void {
		if(btn.node.name.includes('option')){
			let index = Number(btn.node.name.replace('option', ''));
			let type = btn.node.parent.parent.name;
			this.selectOption(type as any, index);
		}else super.$planet_ClickBtn_poof$(btn);
	}

	private select_record: {type: 'symbol'|'elsb', index: number, update: boolean, count: number};
	private hint_text: string;
	private selectOption(type: 'symbol'|'elsb', index: number){
		let record = {type, index, update: true, count: 0};
		if(abd.$planet_same_poof$(this.select_record, record)) return void 0;

		this.$planet_setDetail_poof$(type, index);

		if(!this.select_record) this.select_record = record;
		else Object.assign(this.select_record, record);
	}

	private $planet_unselectOption_poof$(){
		if(!this.select_record) return void 0;
		let {type, index} = this.select_record;
		find(type+'/select/option'+index+'/light', this.$planet_view_poof$.content).active = false;
		find(type+'/select/lbl-option'+index, this.$planet_view_poof$.content).getComponent(Label).color = color(...abd.$planet_color_poof$.$planet_toRGBColor_poof$('#D57C3A'));
		this.select_record = null;
	}

	private $planet_clearOption_poof$(){
		this.$planet_unselectOption_poof$();
		let normal_color = color(...abd.$planet_color_poof$.$planet_toRGBColor_poof$('#D57C3A'));
		['symbol', 'elsb'].forEach(type=>{
			let select = find(type+'/select', this.$planet_view_poof$.content);
			select.parent.getChildByName('detail').getComponent(UIOpacity).opacity = 0;
			select.children.forEach((node, i)=>{
				if(node.name.includes('lbl')) node.getComponent(Label).color = node.name.includes('option') ? normal_color : Color.WHITE;
				else if(node.name.includes('option')) node.getChildByName('light').active = false;
			});

			select.parent.getComponent(UITransform).height = select.parent.children.reduce((acc, node)=>acc + (node.name=='detail' ? 0 : node.getComponent(UITransform).height), 0);
		});
	}

	private $planet_setDetail_poof$(type: 'symbol'|'elsb', index: number){
		let op_detail = find(type+'/select', this.$planet_view_poof$.content).parent.getChildByName('detail').getComponent(UIOpacity);
		let config = $planet_Config_poof$.$planet_otherConfig_poof$[type=='symbol' ? '$planet_freeSymbol_poof$' : '$planet_freeElsb_poof$'][index-1];
		if(!this.select_record){
			find(type+'/select/option'+index+'/light', this.$planet_view_poof$.content).active = true;
			find(type+'/select/lbl-option'+index, this.$planet_view_poof$.content).getComponent(Label).color = Color.WHITE;
		}else{

			find(this.select_record.type+'/select/option'+this.select_record.index+'/light', this.$planet_view_poof$.content).active = false;
			find(this.select_record.type+'/select/lbl-option'+this.select_record.index, this.$planet_view_poof$.content).getComponent(Label).color = color(...abd.$planet_color_poof$.$planet_toRGBColor_poof$('#D57C3A'));

			find(type+'/select/option'+index+'/light', this.$planet_view_poof$.content).active = true;
			find(type+'/select/lbl-option'+index, this.$planet_view_poof$.content).getComponent(Label).color = Color.WHITE;
		}

		let text: string;
		let day = type=='symbol' ? $planet_DC_poof$.$planet_user_poof$.$planet_symbol_day_poof$ : $planet_DC_poof$.$planet_user_poof$.$planet_elsb_day_poof$;
		let current: number;
		let target: number;
		let progress_type: 'symbol'|'elsb'|'level';
		if(type=='symbol' && $planet_DC_poof$.$planet_user_poof$.$planet_symbol_poof$ < config.$planet_symbol_poof$){
			current = $planet_DC_poof$.$planet_parseSymbol_poof$($planet_DC_poof$.$planet_user_poof$.$planet_symbol_poof$);
			target = $planet_DC_poof$.$planet_parseSymbol_poof$(config.$planet_symbol_poof$);

			let [diff, t] = [target-current, target].map(v=>'<color=#FC6A59>'+$planet_DC_poof$.$planet_symbolString_poof$(v)+'</color>');

			text = $planet_GameAssets_poof$.$planet_getLangText_poof$(17, t, diff);
			this.hint_text = $planet_GameAssets_poof$.$planet_getLangText_poof$(14);
			progress_type = 'symbol';
		}else if(day < config.$planet_countElsb_poof$){
			current = day;
			target = config.$planet_countElsb_poof$;
			//text = $planet_GameAssets_poof$.$planet_getLangText_poof$(18, config.$planet_countElsb_poof$, 5);
			this.hint_text = $planet_GameAssets_poof$.$planet_getLangText_poof$(15);
			progress_type = 'elsb';
			if(type=='symbol'){
				text = $planet_GameAssets_poof$.$planet_getLangText_poof$(18, ...[target, 5].map(_=>'<color=#FC6A59>'+_+'</color>'));
				text += '\n' + $planet_GameAssets_poof$.$planet_getLangText_poof$(19, '<color=#FC6A59>'+current+'</color>');
				text += '\n' + $planet_GameAssets_poof$.$planet_getLangText_poof$(20, '<color=#FC6A59>'+$planet_DC_poof$.$planet_user_poof$.$planet_dayElsb_poof$+'</color>');
			}else{
				text = $planet_GameAssets_poof$.$planet_getLangText_poof$(23, '<color=#FC6A59>'+config.$planet_countElsb_poof$+'</color>');
				text += '\n' + $planet_GameAssets_poof$.$planet_getLangText_poof$(24, '<color=#FC6A59>'+current+'</color>');
			}
		}else{
			current = $planet_DC_poof$.$planet_user_poof$.$planet_level_poof$;
			target = config.$planet_level_poof$;

			text = $planet_GameAssets_poof$.$planet_getLangText_poof$(21, '<color=#FC6A59>'+target+'</color>');
			text += '\n' + $planet_GameAssets_poof$.$planet_getLangText_poof$(22, '<color=#FC6A59>'+target+'</color>');
			this.hint_text = $planet_GameAssets_poof$.$planet_getLangText_poof$(16);
			progress_type = 'level';
		}
		op_detail.node.getChildByName('richtext').getComponent(RichText).string = text;
		let bar = op_detail.node.getChildByName('progress').getComponent(ProgressBar);
		bar.progress = abd.$planet_number_poof$.$planet_limit_poof$(current / target, 0, 1);
		if(progress_type=='symbol') bar.node.getChildByName('label').getComponent(Label).string = [Math.min(current, target), target].map(_=>$planet_DC_poof$.$planet_symbolString_poof$(_)).join(' / ');
		else bar.node.getChildByName('label').getComponent(Label).string = `${Math.min(current, target)} / ${target}`;
	}

	private $planet_showDetail_poof$(type: 'symbol'|'elsb'){
		if(this.select_record && this.select_record.type==type && !this.select_record.update) return void 0;

		this.select_record.update = false;
		let ui_container = this.$planet_view_poof$.content.getChildByName(type).getComponent(UITransform);
		let op_detail = ui_container.node.getChildByName('detail').getComponent(UIOpacity);

		let duration = 0.1;
		let target_height = ui_container.node.children.reduce((acc, node)=>acc + node.getComponent(UITransform).height, 0) + 50;
		let target_opacity = 255;

		[ui_container, op_detail].forEach(c=>Tween.stopAllByTarget(c));
		tween(ui_container)
		.to(duration, {height: target_height})
		.set({height: target_height})
		.start();
		tween(op_detail)
		.to(duration, {opacity: target_opacity})
		.set({opacity: target_opacity})
		.start();

		if(type=='symbol') this.$planet_view_poof$.scrollToTop(duration);
		else this.$planet_view_poof$.scrollToBottom(duration);
	}

	private $planet_hideDetail_poof$(type: 'symbol'|'elsb'){
		if(this.select_record && this.select_record.type==type) return void 0;
		let ui_container = this.$planet_view_poof$.content.getChildByName(type).getComponent(UITransform);
		let op_detail = ui_container.node.getChildByName('detail').getComponent(UIOpacity);

		let duration = 0.1;
		let target_height = ui_container.node.children.reduce((acc, node)=>acc + (node.name=='detail' ? 0 : node.getComponent(UITransform).height), 0);
		let target_opacity = 0;

		[ui_container, op_detail].forEach(c=>Tween.stopAllByTarget(c));
		tween(ui_container)
		.to(duration, {height: target_height})
		.set({height: target_height})
		.start();
		tween(op_detail)
		.to(duration, {opacity: target_opacity})
		.set({opacity: target_opacity})
		.start();
	}

	private $planet_renderDeviceUI_poof$(){
		this.$planet_view_poof$.content.getChildByName('device-id').getComponent(Label).string = '['+$planet_Config_poof$.$planet_userID_poof$+']\n'+$planet_Config_poof$.$planet_deviceID_poof$;
	}
}

